<?php

class loginController {
	
	function __construct() {
	}
	
	public function render( $tplname ) {
		$block1 = App::me()->viewPath. DIRECTORY_SEPARATOR .'login'. DIRECTORY_SEPARATOR .$tplname;
		$tpl = App::me()->tpl;
		
		$isAjax = Tools::isAjax();
		$tpl->assign( 'isAjax', $isAjax );
		
		if( $isAjax ) {
			$tpl->display($block1.'.tpl');
			return;
		}
		
		$tpl->assign('title', 'Вход в систему');
		$tpl->assign('block1', $block1);
		$tpl->display('layout1.tpl');
	}
	
	public function index() {		
		// Kill session in any case
		session_start();
		$_SESSION = array();
		if (isset($_COOKIE[session_name()])) {
			setcookie(session_name(), '', time()-42000, '/');
		}
		session_destroy();
		$this->render('form');
		
	}

	public function check() {		
		if( Tools::isAjax() && isset($_POST['uname']) && isset($_POST['passw']) &&
			( $uname=trim(strip_tags($_POST['uname'])) ) === $_POST['uname'] && $uname != '' &&
			( $passw=trim($_POST['passw']) ) === $_POST['passw'] && $passw != '' )
		{
			$model = new login;
			echo json_encode($model->find($uname, $passw));
		}
	}
	
}
?>